package repository

import (
	"github.com/gin-gonic/gin"
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/app/admin/service"
	"ruoyi-go/utils/R"
	"strconv"
)

/**
业务逻辑层
*/

var UserWechatRepo = &wechatRepo{}

type userWechatRepo struct{}

// Save 微信配置保存
func (w *userWechatRepo) Save(c *gin.Context) {
	wechat := system.SysUserWechat{}
	c.BindJSON(&wechat)
	wechat.Id = nil
	loginUserId, _ := c.Get("userId")
	createBy := strconv.Itoa(int(loginUserId.(float64)))
	wechat.CreateBy = &createBy
	service.SysUserWechatService.Save(wechat)
	R.Success(c)
}

// Edit 微信配置修改
func (w *userWechatRepo) Edit(c *gin.Context) {
	wechat := system.SysUserWechat{}
	c.BindJSON(&wechat)
	if wechat.Id == nil {
		panic("id不能为空")
	}
	service.SysUserWechatService.Edit(wechat)
	R.Success(c)
}

// Del 微信配置删除
func (w *userWechatRepo) Del(c *gin.Context) {
	id := c.PostForm("id")
	if id != "" {
		panic("id不能为空")
	}
	i, _ := strconv.ParseInt(id, 10, 64)
	service.SysUserWechatService.Delete(i)
	R.Success(c)
}

// Detail 微信配置详情
func (w *userWechatRepo) Detail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		panic("id不能为空")
	}
	i, _ := strconv.ParseInt(id, 10, 64)
	detail := service.SysUserWechatService.Detail(i)
	R.SuccessData(c, detail)
}

// Page 微信配置删除
func (w *userWechatRepo) Page(c *gin.Context) {
	wechatQo := system.SysUserWechatQO{}
	c.BindJSON(&wechatQo)
	res := service.SysUserWechatService.Page(wechatQo)
	R.SuccessData(c, res)
}
